/*
 *    Copyright 2010-2012 The MyBatis Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.guice;

import static com.google.inject.Guice.createInjector;
import static com.google.inject.name.Names.bindProperties;
import static com.google.inject.name.Names.named;
import static java.lang.System.currentTimeMillis;

import java.io.StringReader;
import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.rules.TestRule;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

import com.google.inject.Binder;
import com.google.inject.Injector;
import com.google.inject.Module;

/**
 * 
 *
 * @version $Id$
 */
abstract class AbstractGuiceTestRunner extends BlockJUnit4ClassRunner {

    private final Injector injector;

    public AbstractGuiceTestRunner(Class<?> klass) throws InitializationError {
        super(klass);

        try {
            final Contact contact = new Contact();
            contact.setFirstName("John");
            contact.setLastName("Doe");
            contact.setCreated(new CustomType(currentTimeMillis()));
            contact.setAddress(null);

            final Contact contactWithAddress = new Contact();
            contactWithAddress.setFirstName("John");
            contactWithAddress.setLastName("Doe");
            contactWithAddress.setCreated(new CustomType(currentTimeMillis()));

            Address address = new Address();
            address.setNumber(1234);
            address.setStreet("Elm street");
            contactWithAddress.setAddress(address);

            final Counter counter = new Counter();

            // bindings
            List<Module> modules = this.createMyBatisModule();
            modules.add(new Module() {
                public void configure(Binder binder) {
                    bindProperties(binder, createTestProperties());
                    binder.bind(Contact.class).toInstance(contact);
                    binder.bind(Contact.class).annotatedWith(named("contactWithAddress")).toInstance(contactWithAddress);
                    binder.bind(Counter.class).toInstance(counter);
                }
            });
            this.injector = createInjector(modules);

            // prepare the test db
            Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration().getEnvironment();
            DataSource dataSource = environment.getDataSource();
            ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
            runner.setAutoCommit(true);
            runner.setStopOnError(true);
            runner.runScript(new StringReader(
                    "DROP TABLE IF EXISTS contact;"
                    + "CREATE TABLE contact (id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1), "
                    + "first_name VARCHAR(20) NOT NULL, "
                    + "last_name VARCHAR(20) NOT NULL, "
                    + "created TIMESTAMP, "
                    + "address VARCHAR(100) DEFAULT NULL) ;"));
            runner.closeConnection();
        } catch (Exception e) {
            throw new InitializationError(e);
        }
    }

    protected abstract List<Module> createMyBatisModule();

    protected abstract Properties createTestProperties();

    @Override
    protected final Object createTest() throws Exception {
        return this.injector.getInstance(this.getTestClass().getJavaClass());
    }

    @Override
    protected List<TestRule> getTestRules(Object target) {
        List<TestRule> rules = super.getTestRules(target);
        rules.add(injector.getInstance(CleanDatabaseRule.class));
        return rules;
    }
}
